package design_pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy implements InvocationHandler {
	private Subject sub;
	public DynamicProxy(Subject sub) {
		this.sub = sub;
	}

	public static void main(String[] args) {
		Subject sub = new RealSubject();
		DynamicProxy proxy = new DynamicProxy(sub);
		Subject obj = (Subject)Proxy.newProxyInstance(RealSubject.class.getClassLoader(), RealSubject.class.getInterfaces(), proxy);
		obj.show();
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("before");
		Object res = method.invoke(sub, args);
		System.out.println("after");
		return res;
	}

}
